package com.davy.gridpoint.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.bumptech.glide.Glide;
import com.davy.gridpoint.R;
import com.davy.gridpoint.http.HttpClient;
import com.davy.gridpoint.utils.LogUtils;
import com.davy.gridpoint.utils.StringUtils;

import java.util.ArrayList;
import java.util.Map;


public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;

    public MainAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_main, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            //陇海线~下行[K1020+800]

            String geo_end_milege = mList.get(position).get("geo_end_milege") + "";
            holder.GeoGridName.setText(mList.get(position).get("line_name") + "~" + mList.get(position).get("xb") + "[K" + mList.get(position).get("geo_start_milege") + (StringUtils.isEmpty(geo_end_milege) ? "" : ("+" + geo_end_milege)) + "]");
            holder.tvTime.setText(mList.get(position).get("createtime") + "");
            holder.tvWz.setText("经度:" + mList.get(position).get("longitude") + "   维度:" + mList.get(position).get("latitude"));

//            holder.delView.setOnClickListener(v -> {
//                if (mItemClick != null) {
//                    mItemClick.delClick(position, mList.get(position));
//                }
//            });
//            holder.imgView.setOnClickListener(v -> {
//                if (mItemClick != null) {
//                    mItemClick.onClick(1, mList.get(position));
//                }
//            });
            holder.dwView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
            holder.itemView.setOnLongClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.delClick(position, mList.get(position));
                }
                return false;
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView GeoGridName, dwView, tvTime, tvWz;

        public ItemViewHolder(View itemView) {
            super(itemView);
            GeoGridName = itemView.findViewById(R.id.GeoGridName);
            dwView = itemView.findViewById(R.id.dwView);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvWz = itemView.findViewById(R.id.tvWz);
        }
    }

    public interface ItemClickListener {
        void onClick(int pos, Map mData);

        void delClick(int pos, Map mData);
    }
}
